﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using cn.sharesdk.unity3d;

public class ShareModel : MonoBehaviour {

    private ShareSDK ssdk;
    
    public GameObject CloseBtn;
    public GameObject WeiXinBtn;
    public GameObject FriendsCircleBtn;
    public GameObject Share;
	
	void Awake () {
        UIEventListener.Get(CloseBtn).onClick = ButtonClick;
        UIEventListener.Get(WeiXinBtn).onClick = ButtonClick;
        UIEventListener.Get(FriendsCircleBtn).onClick = ButtonClick;

        //添加回调
        ssdk = GameObject.Find("SoundManger").GetComponent<ShareSDK>();
        ssdk.authHandler = OnAuthResultHandler;
        ssdk.shareHandler = OnShareResultHandler;
        ssdk.showUserHandler = OnShowUserHandler;
        ssdk.getFriendsHandler = OnGetFriendsHandler;
	}

    void ButtonClick(GameObject button)
    {
        if (button.name == "CloseBtn")
        {
            Destroy(Share);
        }
        else if (button.name == "WeiXinBtn")
        {
            Debug.Log("分享给微信好友");
            ShareContent content = new ShareContent();           
            string downloadUrl = GameObject.Find("UI Root/HallPanel").GetComponent<HallModel>().downloadUrl;            
            content.SetText("快来和我一起游戏吧!下载地址：" + downloadUrl);
            content.SetImagePath("https://f1.webshare.mob.com/code/demo/img/1.jpg");
            content.SetTitle("欢乐掌上棋牌");
            content.SetSite("欢乐掌上棋牌");
            content.SetSiteUrl(downloadUrl);
            content.SetComment("欢乐嗨翻天！");
            content.SetShareType(ContentType.Text);
            ssdk.ShareContent(PlatformType.WeChat, content);
        }
        else if (button.name == "FriendsCircleBtn")
        {
            Debug.Log("分享到朋友圈");
            string downloadUrl = GameObject.Find("UI Root/HallPanel").GetComponent<HallModel>().downloadUrl;
            Debug.Log("downloadUrl = " + downloadUrl);
            ShareContent content = new ShareContent();
            content.SetText("快来和我一起游戏吧！");
            content.SetImagePath("/GUI_Alats_Animation/Publlice/Texture/108x108.png");
            content.SetImageUrl(downloadUrl);
            content.SetTitle("欢乐掌上棋牌");
            //content.SetSite("欢乐掌上棋牌");
            //content.SetSiteUrl(downloadUrl);
            //content.SetComment("欢乐嗨翻天");
            content.SetShareType(ContentType.Text);
            ssdk.ShareContent(PlatformType.WeChatMoments, content); 
        }
    }


    void OnAuthResultHandler(int reqID, ResponseState state, PlatformType type, Hashtable result)
    {

        Debug.Log("授权中");
        //myDebug.text = "授权中";
        if (state == ResponseState.Success)
        {
            print("authorize success !" + "Platform :" + type);
            Debug.Log("授权成功");
            //myDebug.text = "授权成功";
            ssdk.GetUserInfo(PlatformType.WeChat);
        }
        else if (state == ResponseState.Fail)
        {
            #if UNITY_ANDROID
                        print("fail! throwable stack = " + result["stack"] + "; error msg = " + result["msg"]);
            #elif UNITY_IPHONE
			            print ("fail! error code = " + result["error_code"] + "; error msg = " + result["error_msg"]);
            #endif
        }
        else if (state == ResponseState.Cancel)
        {
            print("cancel !");
        }
    }

    void OnShareResultHandler(int reqID, ResponseState state, PlatformType type, Hashtable result)
    {
        if (state == ResponseState.Success)
        {
            print("share successfully - share result :");
            print(MiniJSON.jsonEncode(result));
        }
        else if (state == ResponseState.Fail)
        {
            #if UNITY_ANDROID
                        print("fail! throwable stack = " + result["stack"] + "; error msg = " + result["msg"]);
            #elif UNITY_IPHONE
			            print ("fail! error code = " + result["error_code"] + "; error msg = " + result["error_msg"]);
            #endif
        }
        else if (state == ResponseState.Cancel)
        {
            print("cancel !");
        }
    }

    void OnShowUserHandler(int reqID, ResponseState state, PlatformType type, Hashtable result)
    {

    }

    void OnGetFriendsHandler(int reqID, ResponseState state, PlatformType type, Hashtable result)
    {

    }

}
